﻿using AutoMapper;
using B.S.Material.Domain.BOM;
using B.S.Material.ErrorCode;
using B.S.Material.Read.Api.Applications.Command.MaterialsChildCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Read.Api.Applications.Handler.MaterialsChildHandler
{
    public class GetMaterialsChildHandler : IRequestHandler<GetMaterialsChildCommand, ApiResult<ApiPaging<MaterialsChild>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetMaterialsChildHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<ApiPaging<MaterialsChild>>> Handle(GetMaterialsChildCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<MaterialsChild>> reat = new ApiResult<ApiPaging<MaterialsChild>>();
            var listtt = await db.Queryable<MaterialsChild>().Where(x => !x.IsDel).ToListAsync();

            int totalCount = listtt.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            listtt = listtt.OrderByDescending(a => a.ChildID).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            ApiPaging<MaterialsChild> paging = new ApiPaging<MaterialsChild>()
            {
                totalCount = totalCount,
                PageCount = PageCount,
                Result = listtt.ToList(),

            };

            reat.Code = ApiEnum.显示成功;
            reat.MAG = ApiEnum.显示成功.ToString();
            reat.data = paging;
            return reat;

        }
    }
}
